package cn.saltybe.analysis.crawler.wbtc.secondHandHouse;

import com.geccocrawler.gecco.annotation.PipelineName;
import com.geccocrawler.gecco.pipeline.Pipeline;
import com.geccocrawler.gecco.request.HttpRequest;
import com.geccocrawler.gecco.scheduler.SchedulerContext;

@PipelineName("wbtcSecondHandHouseListPipeline")
public class WbtcSecondHandHouseListPipeline implements Pipeline<WbtcSecondHandHouseList> {
    @Override
    public void process(WbtcSecondHandHouseList wbtcSecondHandHouseList) {
        HttpRequest currentRequest = wbtcSecondHandHouseList.getRequest();
        String province = currentRequest.getParameter("province");
        String city = currentRequest.getParameter("city");
        System.out.println("正进行 " + province + " " + city + " 的二手房列表扫描，当前第 " + wbtcSecondHandHouseList.getCurrentPage() + " 页");
        // 在这里进行抓取到信息的逻辑处理
        int subRequestCount = 0;
        for(String h: wbtcSecondHandHouseList.getHouseList()) {
            //if (subRequestCount >= 1) break;
            //System.out.println("获取到二手房链接：" + h);
            HttpRequest subRequest = currentRequest.subRequest(h);
            subRequest.addParameter("province", currentRequest.getParameter("province"));
            subRequest.addParameter("city", currentRequest.getParameter("city"));
            SchedulerContext.into(subRequest);
            subRequestCount++;
        }
        System.out.println("已将 " + subRequestCount + " 个请求加入队列。");
        // 爬取下一页
        if (wbtcSecondHandHouseList.getNextPageUrl() != null) {
            HttpRequest subRequest = currentRequest.subRequest(wbtcSecondHandHouseList.getNextPageUrl());
            subRequest.addParameter("province", currentRequest.getParameter("province"));
            subRequest.addParameter("city", currentRequest.getParameter("city"));
            SchedulerContext.into(subRequest);
            System.out.println("已将下一页请求加入队列。");
        }
    }
}
